package com.sau.pixelcart.controller;

import com.sau.pixelcart.common.result.PageResult;
import com.sau.pixelcart.common.result.Result;
import com.sau.pixelcart.domain.dto.CategoryDTO;
import com.sau.pixelcart.domain.dto.CategoryPageDTO;
import com.sau.pixelcart.domain.vo.CategoryVO;
import com.sau.pixelcart.service.ICategoryService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/category")
@Slf4j
public class CategoryController {

    @Autowired
    private ICategoryService categoryService;
    /**
     * 分类分页查询
     * @param categoryPageDTO
     * @return
     */
    @GetMapping("/list")
    public Result list(CategoryPageDTO categoryPageDTO) {
        log.info("分类分页查询...{}", categoryPageDTO);
        PageResult<CategoryVO> pageResult = categoryService.queryPage(categoryPageDTO);
        return Result.success(pageResult);
    }

    /**
     * 添加/修改分类
     * @param categoryDTO
     * @return
     */
    @PostMapping("/save")
    public Result save(@RequestBody CategoryDTO categoryDTO){
        log.info("添加/修改分类...{}", categoryDTO);
        categoryService.saveOrUpdate(categoryDTO);
        return Result.success();
    }

    /**
     * 删除分类
     * @param id
     * @return
     */
    @DeleteMapping("/{id}")
    public Result delete(@PathVariable String id){
        log.info("删除分类，{}",id);
        categoryService.deleteById(id);
        return Result.success();
    }

    @GetMapping("/getAllCategory")
    public Result getAllCategory(){
        log.info("开始查询商品分类");
        List<CategoryVO> categoryVOList =  categoryService.getAllCategory();
        return Result.success(categoryVOList);
    }

}
